package com.freight.tws.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.exception.ServiceException;
import com.freight.common.core.utils.StringUtils;
import com.freight.common.core.utils.poi.ExcelUtil;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.web.page.Condition;
import com.freight.common.core.web.page.Query;
import com.freight.common.core.web.page.TableDataInfo;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.security.annotation.PreAuthorize;
import com.freight.tws.domain.OrderGis;
import com.freight.tws.domain.vo.GisVO;
import com.freight.tws.service.IOrderGisService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 轨迹Controller
 * 
 * @author wms
 * @date 2021-06-30
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/orderGis")
public class OrderGisController extends BaseController
{
    private final IOrderGisService iOrderGisService;

    /**
     * 查询轨迹列表
     */
    @PreAuthorize(hasPermi="tws:orderGis:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, OrderGis orderGis)
    {
        LambdaQueryWrapper<OrderGis> lqw = new LambdaQueryWrapper<OrderGis>();
        if (orderGis.getCarId() != null){
            lqw.eq(OrderGis::getCarId, orderGis.getCarId());
        }
        if (StringUtils.isNotBlank(orderGis.getPositions())){
            lqw.eq(OrderGis::getPositions, orderGis.getPositions());
        }
        if (StringUtils.isNotBlank(orderGis.getPlateNumber())){
            lqw.eq(OrderGis::getPlateNumber, orderGis.getPlateNumber());
        }
        if (orderGis.getOrderId() != null){
            lqw.eq(OrderGis::getOrderId, orderGis.getOrderId());
        }
        IPage<OrderGis> list = iOrderGisService.page(Condition.getPage(query),lqw);
        return getDataTable(list);
    }

    /**
     * 导出轨迹列表
     */
    @PreAuthorize(hasPermi = "tws:orderGis:export")
    @Log(title = "轨迹", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderGis orderGis) throws IOException
    {
        LambdaQueryWrapper<OrderGis> lqw = new LambdaQueryWrapper<OrderGis>(orderGis);
        List<OrderGis> list = iOrderGisService.list(lqw);
        ExcelUtil<OrderGis> util = new ExcelUtil<OrderGis>(OrderGis. class);
        util.exportExcel(response, list, "gis");
    }

    /**
     * 获取轨迹详细信息
     */
    @PreAuthorize(hasPermi="tws:orderGis:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iOrderGisService.getById(id));
    }

    /**
     * 新增轨迹
     */
    @PreAuthorize(hasPermi="tws:orderGis:add")
    @Log(title = "轨迹", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderGis orderGis) {
        return toAjax(iOrderGisService.save(orderGis) ? 1 : 0);
    }

    /**
     * 修改轨迹
     */
    @PreAuthorize(hasPermi="tws:orderGis:edit")
    @Log(title = "轨迹", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderGis orderGis) {
        return toAjax(iOrderGisService.updateById(orderGis) ? 1 : 0);
    }

    /**
     * 删除轨迹
     */
    @PreAuthorize(hasPermi="tws:orderGis:remove")
    @Log(title = "轨迹", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iOrderGisService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 根据订单查询轨迹
     * @param orderId
     * @return
     */
    @GetMapping(value = "/getByOrder/{orderId}")
    public List<GisVO> getByOrder(@PathVariable("orderId") String orderId){
        OrderGis gis = iOrderGisService.lambdaQuery().eq(OrderGis::getOrderId, orderId).one();
        if(gis == null) {
            throw new ServiceException("暂无轨迹");
        }
        String positions = gis.getPositions();
        return JSONArray.parseArray("["+positions+"]",GisVO.class);
    }
}
